"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _jsxRuntime = require("react/jsx-runtime");

var _icons = require("@ant-design/icons");

var _antdV = require("antd-v4");

var _classnames = _interopRequireDefault(require("classnames"));

var _useMergedState3 = _interopRequireDefault(require("rc-util/lib/hooks/useMergedState"));

require("./index.less");

var _excluded = ["label", "key"];

var HeaderMenu = function HeaderMenu(props) {
  var _props$items = props.items,
      items = _props$items === void 0 ? [] : _props$items,
      _props$type = props.type,
      type = _props$type === void 0 ? 'inline' : _props$type,
      prefixCls = props.prefixCls,
      propActiveKey = props.activeKey;

  var _useMergedState = (0, _useMergedState3.default)(propActiveKey, {
    value: propActiveKey,
    onChange: props.onChange
  }),
      _useMergedState2 = (0, _slicedToArray2.default)(_useMergedState, 2),
      activeKey = _useMergedState2[0],
      setActiveKey = _useMergedState2[1];

  if (items.length < 1) {
    return null;
  }

  var activeItem = items.find(function (item) {
    return item.key === activeKey;
  }) || items[0];

  if (type === 'inline') {
    return (0, _jsxRuntime.jsx)("div", {
      className: (0, _classnames.default)("".concat(prefixCls, "-menu"), "".concat(prefixCls, "-inline-menu")),
      children: items.map(function (item, index) {
        return (0, _jsxRuntime.jsx)("div", {
          onClick: function onClick() {
            setActiveKey(item.key);
          },
          className: (0, _classnames.default)("".concat(prefixCls, "-inline-menu-item"), activeItem.key === item.key ? "".concat(prefixCls, "-inline-menu-item-active") : undefined),
          children: item.label
        }, item.key || index);
      })
    });
  }

  if (type === 'tab') {
    return (0, _jsxRuntime.jsx)(_antdV.Tabs, {
      activeKey: activeItem.key,
      onTabClick: function onTabClick(key) {
        return setActiveKey(key);
      },
      children: items.map(function (_ref, index) {
        var label = _ref.label,
            key = _ref.key,
            rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);
        return (0, _jsxRuntime.jsx)(_antdV.Tabs.TabPane, (0, _objectSpread2.default)({
          tab: label
        }, rest), key || index);
      })
    });
  }

  return (0, _jsxRuntime.jsx)("div", {
    className: (0, _classnames.default)("".concat(prefixCls, "-menu"), "".concat(prefixCls, "-dropdownmenu")),
    children: (0, _jsxRuntime.jsx)(_antdV.Dropdown, {
      trigger: ['click'],
      overlay: (0, _jsxRuntime.jsx)(_antdV.Menu, {
        selectedKeys: [activeItem.key],
        onClick: function onClick(item) {
          setActiveKey(item.key);
        },
        items: items.map(function (item, index) {
          return {
            key: item.key || index,
            disabled: item.disabled,
            label: item.label
          };
        })
      }),
      children: (0, _jsxRuntime.jsxs)(_antdV.Space, {
        className: "".concat(prefixCls, "-dropdownmenu-label"),
        children: [activeItem.label, (0, _jsxRuntime.jsx)(_icons.DownOutlined, {})]
      })
    })
  });
};

var _default = HeaderMenu;
exports.default = _default;